/**
 * 项目名称: Topic_897.cpp
 * 项目路径: study/DataStructure/LeetCode/树/简单
 * 编辑时间: 2024年08月23日07时59分30秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

//class Solution {
//public:
//    vector<TreeNode*>vn;
//    void pre(TreeNode* root){
//        if(root){
//            pre(root->left);
//            vn.push_back(root);
//            pre(root->right);
//        }
//    }
//    TreeNode* increasingBST(TreeNode* root) {
//        if(!root)return root;
//        pre(root);
//        TreeNode * ans = new TreeNode(-1);
//        TreeNode* curr = ans;
//        for(auto x:vn){
//            curr->right = new TreeNode(x->val);
//            curr = curr->right;
//        }
//        return ans->right;
//    }
//};
class Solution {
public:
    TreeNode* resNode;
    void pre(TreeNode* root){
        if(!root)return;
        pre(root->left);

        //中序遍历过程改变节点指向
        resNode->right = root;
        root->left = nullptr;
        resNode = root;

        pre(root->right);
    }
    TreeNode* increasingBST(TreeNode* root) {
       TreeNode* dummyNode = new TreeNode(-1);
       resNode = dummyNode;
        pre(root);
        return dummyNode->right;
    }
};